package com.api.gbhg.huangwm.BxActionV2;

import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-11-07
 * @Version 1.0
 * @Description TODO 明细报销退回时释放项目已冻结的经济分类额度
 **/
public class ReleaseQuotaForRejectDetails implements Action {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/BxActionV2/ReleaseQuotaForRejectDetails",false);
    private BxActionUtils utils = new BxActionUtils();


    @Override
    public String execute(RequestInfo requestInfo) {
        int requestid = requestInfo.getRequestManager().getRequestid();
        logTool.writeLog("报销单requestid:"+requestid+" 进入退回后逻辑");
        String src = requestInfo.getRequestManager().getSrc().toLowerCase();
        if(!src.equals("reject")){
            requestInfo.getRequestManager().setMessagecontent("Action[ReleaseQuotaForRejectDetails]只能在退回中使用,当前src为 "+src);
            requestInfo.getRequestManager().setMessageid(requestid+"");
            return FAILURE_AND_CONTINUE;
        }
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        int billid = requestInfo.getRequestManager().getBillid();
        //获取明细1表名
        DetailTable[] detailTables = requestInfo.getDetailTableInfo().getDetailTable();
        String dtTable_0 = detailTables[0].getTableDBName();
        User user = new User(requestInfo.getRequestManager().getCreater());
        int subid = user.getUserSubCompany1();

        //获取明细1数据集
        List<Map<String,Object>> datas = utils.getDetailsInfo_checkCreate(dtTable_0,billid);
        //更新数据用
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        //释放预算项目额度
        try {
            //第一步 释放经费表冻结的额度和使用单位冻结的额度
            boolean result1 = utils.releaseJingFei(rst,datas);
            boolean result2 = utils.releaseSydwed(rst,datas,subid);
            if(!result1){
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("明细报销退回经办人释放预算经济分类额度失败");
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
            if(!result2){
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("明细报销退回经办人释放使用单位额度失败");
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
            RecordSet rs = new RecordSet();
            //第二步 清除报销记录
            rs.execute("delete from uf_bxsjjl where lcid="+requestid);
            rs.execute("delete from uf_bxjl_kj where lcid="+requestid);
            rs.execute("delete from uf_bxjl_kj_dt1 where lcid="+requestid);
            rs.execute("delete from uf_bxjl_kj_dt2 where lcid="+requestid);
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("明细报销退回经办人释放预算经济分类额度出现异常 "+e.getMessage());
            requestInfo.getRequestManager().setMessageid(requestid+"");
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return SUCCESS;
    }
}
